Meteor ফ্রেমওয়ার্ক MongoDB ডাটাবেসের সাথে একত্রে কাজ করতে অত্যন্ত সুবিধাজনক। Meteor স্বয়ংক্রিয়ভাবে MongoDB ডাটাবেসের সাথে সংযুক্ত হয়ে CRUD (Create, Read, Update, Delete) অপারেশন করতে সহায়ক। এখানে Meteor ব্যবহার করে MongoDB-এ CRUD অপারেশন কীভাবে করা যায়, তা বর্ণনা করা হলো।
প্রথম ধাপ: Meteor প্রজেক্ট তৈরি করা
Step 1: Meteor ইনস্টল করুন। যদি Meteor আপনার সিস্টেমে ইনস্টল না থাকে, তাহলে নিচের কমান্ড ব্যবহার করে এটি ইনস্টল করুন:
curl https://install.meteor.com/ | shStep 2: একটি নতুন Meteor প্রজেক্ট তৈরি করুন:
meteor create crud-example cd crud-example
MongoDB সেটআপ
Meteor ডিফল্টভাবে MongoDB ব্যবহার করে, তাই MongoDB এর আলাদা কোনো সেটআপ করার প্রয়োজন নেই। Meteor নিজেই MongoDB এর সাথে সংযুক্ত হয়।
CRUD অপারেশন
১. Create (ডেটা তৈরি করা)
MongoDB-তে নতুন ডকুমেন্ট (বা রেকর্ড) যুক্ত করতে insert বা insertOne ব্যবহার করা হয়। Meteor এর ক্ষেত্রে, ডেটা Meteor.methods() বা Collections.insert() ব্যবহার করে ইনসার্ট করা যায়।
Example: একটি সিম্পল কলেকশন তৈরি এবং ডেটা ইনসার্ট করা।
// imports/api/items.js
import { Mongo } from 'meteor/mongo';
export const Items = new Mongo.Collection('items');
// Meteor.methods এর মাধ্যমে ডেটা ইনসার্ট করা
Meteor.methods({
  'items.insert'(name, price) {
    Items.insert({
      name,
      price,
      createdAt: new Date(),
    });
  },
});
এখানে items.insert() মেথড কল করে MongoDB-তে নতুন আইটেম যুক্ত করা হবে।
২. Read (ডেটা পড়া)
MongoDB-তে ডেটা পড়তে find() বা findOne() মেথড ব্যবহার করা হয়। Meteor এর ক্ষেত্রে, MongoDB থেকে ডেটা পড়তে find() মেথড ব্যবহার করতে হবে।
Example: MongoDB থেকে সকল আইটেম পড়া:
// imports/api/items.js
import { Mongo } from 'meteor/mongo';
export const Items = new Mongo.Collection('items');
// আইটেমগুলি পাওয়া
Meteor.publish('items', function() {
  return Items.find();
});
এই কোডটি ডেটাবেসের items কালেকশন থেকে সমস্ত ডকুমেন্ট ফেচ করে, এবং এটি publish করে, যা ক্লায়েন্ট সাইডে subscribe করতে পারে।
Client Side Example:
// client/main.js
import { Meteor } from 'meteor/meteor';
import { Items } from '../imports/api/items';
Meteor.subscribe('items');
// ব্যবহারকারীকে আইটেম প্রদর্শন
Template.body.helpers({
  items() {
    return Items.find();
  },
});
৩. Update (ডেটা আপডেট করা)
MongoDB-তে একটি ডকুমেন্ট আপডেট করতে update() মেথড ব্যবহার করা হয়। Meteor এ update() মেথডের মাধ্যমে নির্দিষ্ট আইটেমের ডেটা আপডেট করা যেতে পারে।
Example: আইটেমের মূল্য আপডেট করা:
// imports/api/items.js
import { Mongo } from 'meteor/mongo';
export const Items = new Mongo.Collection('items');
Meteor.methods({
  'items.update'(itemId, newPrice) {
    Items.update(itemId, {
      $set: { price: newPrice },
    });
  },
});
এখানে items.update() মেথড দিয়ে কোনো নির্দিষ্ট আইটেমের মূল্য আপডেট করা হয়েছে।
৪. Delete (ডেটা মুছতে)
MongoDB-তে ডেটা মুছতে remove() মেথড ব্যবহার করা হয়। Meteor-এ remove() ব্যবহার করে ডেটা মুছে ফেলা যায়।
Example: একটি আইটেম মুছতে:
// imports/api/items.js
import { Mongo } from 'meteor/mongo';
export const Items = new Mongo.Collection('items');
Meteor.methods({
  'items.remove'(itemId) {
    Items.remove(itemId);
  },
});
এখানে items.remove() মেথডের মাধ্যমে নির্দিষ্ট আইটেমটি MongoDB থেকে মুছে ফেলা হয়েছে।
Complete Example:
// imports/api/items.js
import { Mongo } from 'meteor/mongo';
export const Items = new Mongo.Collection('items');
Meteor.methods({
  'items.insert'(name, price) {
    Items.insert({
      name,
      price,
      createdAt: new Date(),
    });
  },
  'items.update'(itemId, newPrice) {
    Items.update(itemId, {
      $set: { price: newPrice },
    });
  },
  'items.remove'(itemId) {
    Items.remove(itemId);
  },
});
Meteor.publish('items', function() {
  return Items.find();
});
Client Side Example:
<!-- client/main.html -->
<head>
  <title>CRUD Example</title>
</head>
<body>
  <div>
    <h1>Items List</h1>
    <ul>
      {{#each items}}
        <li>{{name}} - {{price}} 
          <button class="remove" data-id="{{_id}}">Remove</button>
        </li>
      {{/each}}
    </ul>
    <form>
      <input type="text" name="name" placeholder="Item Name">
      <input type="number" name="price" placeholder="Item Price">
      <button type="submit">Add Item</button>
    </form>
  </div>
</body>
<script>
  import { Meteor } from 'meteor/meteor';
  import { Items } from '../imports/api/items';
  Meteor.subscribe('items');
  Template.body.helpers({
    items() {
      return Items.find();
    },
  });
  Template.body.events({
    'submit form'(event) {
      event.preventDefault();
      const target = event.target;
      const name = target.name.value;
      const price = target.price.value;
      Meteor.call('items.insert', name, price);
      target.name.value = '';
      target.price.value = '';
    },
    'click .remove'(event) {
      const itemId = event.target.getAttribute('data-id');
      Meteor.call('items.remove', itemId);
    },
  });
</script>
সারাংশ
Meteor এর মাধ্যমে MongoDB-তে CRUD অপারেশন করা খুবই সহজ এবং সরল। Meteor স্বয়ংক্রিয়ভাবে MongoDB সংযোগ স্থাপন করে এবং ডেটা সিঙ্ক্রোনাইজেশন, সার্ভার এবং ক্লায়েন্ট সাইডে CRUD অপারেশন পরিচালনা করতে সহায়ক। এই কোডের মাধ্যমে আপনি MongoDB-এ Create, Read, Update, এবং Delete অপারেশন সিম্পলভাবে সম্পাদন করতে পারবেন।
Read more